<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('素材分配')" />
</head>
<body class="gray-bg">
<div class="container-div">
	<div class="row">
		<div class="col-sm-12 search-collapse">
			<form id="role-form">
				<input type="hidden" id="id" name="id" th:value="${id}">
				<div class="select-list">
					<ul>
						<li>
							<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
							<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
						</li>
					</ul>
				</div>
			</form>
		</div>

		<div class="btn-group-sm" id="toolbar" role="group">
			<a class="btn btn-success" onclick="selectMaterial()">
				<i class="fa fa-plus"></i> 添加素材
			</a>
			<a class="btn btn-danger multiple disabled" onclick="deleteMaterialBatch()">
				<i class="fa fa-remove"></i> 批量删除
			</a>
			<a class="btn btn-warning" onclick="closeItem()">
				<i class="fa fa-reply-all"></i> 关闭
			</a>
		</div>

		<div class="col-sm-12 select-table table-striped">
			<table id="bootstrap-table"></table>
		</div>
	</div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var imageReg = /.+\.(gif|jpg|jpeg|png|webp|bmp)/i;
    var auditStateDatas = [[${@dict.getType('audit_state')}]];
    var useStateDatas = [[${@dict.getType('use_state')}]];
    var materialTypeDatas = [[${@dict.getType('material_type')}]];

    var prefix = ctx + "cms/product"

    $(function() {
        var options = {
            url: prefix + "/materialList",
            pagination: false,
            queryParams: queryParams,
            sortName: "createTime",
            sortOrder: "desc",
            modalName: "图片",
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'groupId',
                    title: '分组id',
                },
                {
                    field: 'siteId',
                    title: '站点id'
                },
                {
                    field: 'materialName',
                    title: '素材名称'
                },
                {
                    field: 'materialType',
                    title: '素材类型 字典 1图片2视频',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(materialTypeDatas, value);
                    }
                },
                {
                    field: 'description',
                    title: '素材描述'
                },
                {
                    field: 'materialSize',
                    title: '素材大小'
                },
                {
                    field: 'savePath',
                    title: '保存路径',
                    formatter: function(value, row, index) {
                        return $.table.imageView(row.savePath);
                    }
                },
                {
                    field: 'width',
                    title: '分辨率 宽'
                },
                {
                    field: 'height',
                    title: '分辨率 高'
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    field: 'sort',
                    title: '排序'
                },
                {title: '操作', align: 'center', formatter: function(value, row, index) {
                        var actions = [];
						if (row.type == 2) {
							actions.push('<a class="btn btn-primary  btn-xs" href="javascript:void(0)" onclick="setMain(' + row.id + ',' + row.productId +  ')">设为主图</a>');
						}
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="deleteMaterial(\'' + row.id +  '\')"><i class="fa fa-remove"></i>删除</a>');


                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });

	//  格式化文件大小
	function renderFileSize(value){
		if(null==value||value==''){
			return "0 Bytes";
		}
		var unitArr = new Array("Bytes","KB","MB","GB","TB","PB","EB","ZB","YB");
		var index=0,
				srcsize = parseFloat(value);
		index=Math.floor(Math.log(srcsize)/Math.log(1024));
		var size =srcsize/Math.pow(1024,index);
		//  保留的小数位数
		size=size.toFixed(2);
		return size+unitArr[index];
	}

    function queryParams(params) {
        var search = $.table.queryParams(params);
        search.productId = $("#productId").val();
        return search;
    }

    /* 分配素材 */
    function selectMaterial() {
        var url = prefix + '/selectMaterial/' + $("#id").val();
        $.modal.open("选择素材", url);
    }

    /* 分配用户-批量取消 */
    function deleteMaterialBatch() {
        var rows = $.table.selectFirstColumns();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
            var data = {  "ids": rows.join(), "id": $("#id").val() };
            $.operate.submit(prefix + "/deleteMaterialBatch", "post", "json", data);
        });
    }

    /* 分配用户-取消授权 */
    function deleteMaterial(id) {
        $.modal.confirm("确认要取消关联该素材吗？", function() {
            $.operate.post(prefix + "/deleteMaterialBatch", {  "ids": id, "id": $("#id").val() });
        })
    }

</script>
</body>
</html>